<?php
namespace app\mobile\controller;

use app\model\Record; 
use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Fans; 
use app\model\Relation; 

class Saveinfo extends Base
{
    public function Saveinfo()
    {
global $_G, $_P;
$fans = $this->checkuser();
$system    = $_G['system'];
$op        = !empty($_P['op']) ? $_P['op'] : 'display';
$themeid   = intval($_P['themeid']);
if($themeid < 0){
    return_msg('活动入口不正确');
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    die(Util::showError("请确认活动地址是否正确"));
    return;
}
$config = 	Theme::getConfig($themeid);
$joinid   = intval($_P['joinid']);
$recordid   = intval($_P['recordid']);
$type      = $_P['type'];
$forward   = $_P['forward'];
$pm = array(":themeid"=>$themeid);
$p = array(":openid"=>$_G['openid'],":themeid"=>$themeid);

if($op == 'ready'){
    if($theme['status']==0){
        $result=  array();
        $result['errcode'] = 1;
        $result['msg'] = "活动未开始";
        return_msg($result, '', 'ajax');
    }
    if($theme['status']==2){
        $result=  array();
        $result['errcode'] = 1;
        $result['msg'] = "活动已结束";
        return_msg($result, '', 'ajax');
    }
	$uid = $_G['member']['uid'];
    $dbfans = Fans::getByTheme($themeid,$_G['openid']);
	if($dbfans['status']!=1){
		$result=  array();
		$result['errcode'] = 1;
		$result['msg'] = '请稍后再试';
		return_msg($result, '', 'ajax');
	}
 //    $workerid = intval($_P['workerid']);
 //    if ($workerid==0){
 //        $relation = Relation::getByThemeId($themeid,$_G['openid']);
 //        $workerid = $relation['workerid'];
 //    }
 //    $fromuid = intval($_P['fromuid']);
	// $fopenid = $_P['fopenid'];
	// if($fromuid == $_G['member']['uid'] || $fopenid == $_G['openid']){
	//     $fromuid = 0;
	//     $fopenid = "";
	// }
	// if($fopenid == "" || $fromuid == 0){
        $isupdaterelation=false;
        $relation = Relation::getByThemeId($themeid,$_G['openid']);
        if(!empty($relation)){
            $fromuid = $relation['parentid'];
            $fopenid = $relation['popenid'];
            $workerid = $relation['workerid'];
        }

        $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
        if (empty($myworker)){
            //如果来源为员工,则业绩给员工
            if (!empty($fromuid)){
                $theyworker = Worker::getThemeWorkerByUid($fromuid,$themeid,1);
                if (!empty($theyworker)){
                    $fromuid = $theyworker['uid'];
                    $fopenid = $theyworker['openid'];
                    $workerid = $theyworker['uid'];
                    $isupdaterelation=true;
                }
            }
            //如果来源为空,则给员工
            if (empty($fromuid) && !empty($workerid)){
                $theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
                $fromuid = $theyworker['uid'];
                $fopenid = $theyworker['openid'];
                $isupdaterelation=true;
            }
            //如果员工为空,则给来源所属员工
            if (!empty($fopenid) && empty($workerid)){
                $theyfrom = Relation::getByThemeId($themeid,$fopenid);
                $workerid = $theyfrom['workerid'];
                $isupdaterelation=true;
            }

            //在没有开启附近团的情况,当团员报名时,员工业绩归属与团长一致
            if ($config['isrecent']==0 && $recordid > 0 && $joinid == 1){
                $record = Record::getAttendGroupById($recordid);
                if (!empty($record['workerid'])){
                    $workerid = $record['workerid'];
                    $isupdaterelation=true;
                }
            }
            if ($isupdaterelation==true){
                    Relation::update($relation['id'],array('parentid' => $fromuid,'popenid' => $fopenid,'workerid' => $workerid));
            }
        }else{
            $fromuid = $_G['member']['uid'];
            $fopenid = $_G['openid'];
            $workerid = $_G['member']['uid'];
        }

    //}
	$username = $_P['username'];
	$mobile   = $_P['mobile'];
	$option   = $_P['option'];
	$address  = $_P['address'];
	$code     = $_P['code'];
	$datetime = $_P['datetime'];
	if(empty($username)){
	    $result['errcode'] = 1;
	    $result['msg'] = '姓名为空';
	    return_msg($result, '', 'ajax');
	}elseif(empty($mobile)){
	    $result['errcode'] = 1;
	    $result['msg'] = '电话为空';
	    return_msg($result, '', 'ajax');
	}
	if(empty($system['appid']) || empty($system['mchid'] )){
		$result=  array();
		$result['errcode'] = 1;
		$result['msg'] = '支付参数未正确设置';
		return_msg($result, '', 'ajax');
	}
	$fee  = 0;
	$pid  = 0;

    //load_model('mc');
    member_update($_G['member']['uid'],array('mobile' => $mobile,'realname'=>$username));

    //自动更新档案信息
    $data = array(
        'username' => $username,
        'mobile'   => $mobile,
        'addtime'  => TIMESTAMP,
        'address'  => $address,
        'datetime' => $datetime,
        'option'   => $option,
        'fromuid'  => $fromuid,
        'fopenid'  => $fopenid,
        'workerid'  => $workerid,
    );
    if ($system['hdget']==1) {
        if (Fans::getuid_old($fromuid,$themeid)) {
            $data['fromuid_old']  = $fromuid;
            $data['fopenid_old']  = $fopenid;
        }
        if (Fans::getuid_old($workerid,$themeid)) {
            $data['workerid_old']  = $workerid;
        }
    }
    $r = db_update(t_fans,$data,array("uid"=>$_G['member']['uid']));
    $uncode = $themeid.'-'.TIMESTAMP.rand(1000,1000000);
    $joinid == 9?$type = 9:$type = 0;
    $data = array(
        'uniacid'  => $_G['mpid'],
        'themeid'  => $themeid,
        'openid'   => $_G['openid'],
        'uid'      => $_G['member']['uid'],
        'nickname' => $fans['nickname'],
        'avatar'   => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
        'username' => $username,
        'mobile'   => $mobile,
        'addtime'  => TIMESTAMP,
        'fee'      => $fee,
        'address'  => $address,
        'datetime' => $datetime,
        'option'   => $option,
        'code'     => $code,
        'uncode'   => $uncode,
        'status'   => 0,
        'fromuid'  => $fromuid,
        'fopenid'  => $fopenid,
        'workerid'  => $workerid,
        'pid'      => $pid,
        'type'     => $type,
    );
    if ($system['hdget']==1) {
        if (Fans::getuid_old($fromuid,$themeid)) {
            $data['fromuid_old']  = $fromuid;
            $data['fopenid_old']  = $fopenid;
        }
        if (Fans::getuid_old($workerid,$themeid)) {
            $data['workerid_old']  = $workerid;
        }
    }
    // if($fee ==0 && $type == 0){
    //     $data['status'] = 1;
    // }
    
    $p3 = $p;
    $p3[':type'] = $type;
    $record = db_find ( "SELECT * FROM " . tablename (t_record) . "
                                    WHERE themeid =:themeid
                                            and openid = :openid and type = :type  order by id desc limit 1",$p3);

    if(empty($record)){
        $r = $insertid = db_insert(t_record,$data);
        if(!empty($r)){
            $recordid = $insertid;
        }else{
            $result=  array();
            $result['errcode'] = 1;
            $result['msg'] = '请重新下单';
            return_msg($result, '', 'ajax');
        }

        if($fee ==0 && $type == 0){
            $url = $_G['siteroot'].createMobileUrl('index',array('themeid'=>$themeid,'recordid'=>$recordid));
            if( $system['noticeopen'] == 1){
                $message = array(
                    "url"       =>$url,
                    "openid"    =>$_G['openid'],
                    "first"     =>$fans['username'].'您好，您已成功报名'.$djm,
                    "keyword1"  =>$theme['title'],
                    "keyword2"  =>$theme['jubf'],
                    "keyword3"  =>date('Y-m-d',$theme['begintime']).'~'.date('Y-m-d',$theme['endtime']),
                    "remark"    =>!empty($system['attendok_remark']) ?$system['attendok_remark']:'感谢你的参与，点击查看活动详情,客服电话:'.$theme['mobile'],
                );
                Util::sendAttendNotice($message,$system);
            }
            
            $result['errcode'] = 110;
            $result['msg'] = '报名成功！';
            $result['url'] = $url;
            return_msg($result, '', 'ajax');
        }
    }else{
            $result['errcode'] = 1;
            $result['msg'] = '请不要重复报名！';
            $result['url'] = $url;
            return_msg($result, '', 'ajax');
    }
}










}
}